<?php

	if ( isset ( $_GET['title'] ) ) $title = $_GET['title'];
	if ( isset ( $_GET['year'] ) ) $year = $_GET['year'];
	if ( isset ( $_GET['genre'] ) ) $genre = $_GET['genre'];
	if ( isset ( $_GET['actor'] ) ) $actor = $_GET['actor'];
	if ( isset ( $_GET['director'] ) ) $director = $_GET['director'];
	
	$db = new DBManager ( 'localhost', 'root', '', 'iumus' );
	$query = 'SELECT id, title, year, genre, actors, director, cover FROM movie WHERE ';
	if ( isset ( $title ) ) $query .= 'title  LIKE "%'.$title.'%" AND ';
	if ( isset ( $year ) && strlen ( $year ) > 0 ) $query .= 'year = "'.$year.'" AND ';
	if ( isset ( $genre ) && count ( $genre ) > 0 ) {
		$query .= '( ';
		for ( $i = 0; $i < count ( $genre ); $i++ )
			$query .= 'genre LIKE "%'.$genre[$i].'%" OR ';
		$query .= '1 = 2 ) AND ';
	}
	if ( isset ( $actor ) && strlen ( $actor ) > 0) $query .= 'actors = "'.$actor.'" AND ';
	if ( isset ( $director ) && strlen ( $director ) > 0) $query .= 'director LIKE "%'.$director.'%" AND ';
	$query .= '1 = 1';

	$result = $db->executeQuery ( $query );
	echo '<table>';
	echo '<tr><th colspan="3">Search results: '.mysql_num_rows($result).' articles (<a href="javascript:showAdvancedSearch()">Advanced Search</a>)</th></tr>';
	
	echo '<tr><td colspan="3">';
	echo '<div id="advanced" style="display: none">';
	include 'support/AdvancedSearch.html';
	echo '</div>';
	echo '</td></tr>';
	
	//echo $query;
	
	while ( $row = mysql_fetch_array ( $result ) ) {
		echo '<tr>';
		echo '	<td>';
		if ( $row['cover'] == 'N/A' ) $row['cover'] = 'wp-content/themes/theme1273/images/noCover.png';
		echo '		<a href="infoFilm.php?id='.$row['id'].'">';
		echo '		<img src="'.$row['cover'].'" style="width: 43px; height: 65px"/>';
		echo '		</a>';
		echo '	</td>';
		echo '	<td>';
		echo '		<a  style="font-size: 13px; color: #FF5000; font-weight: bold;" href="infoFilm.php?id='.$row['id'].'">';
		echo '		'.$row['title'].' </a></br>'.$row['genre'].'';
		//echo '		</a>';
		echo '	</td>';
		echo '	<td>';
		echo '		'.$row['year'];
		echo '	</td>';
		echo '</tr>';
	}
	
	echo '</table>';
	$contTil = '"Search Result For -';
	if ( isset ( $_GET['title'] ) )
	 	if ( strlen($title) > 0 ) 
	 		$contTil .= ' Title: '.$title;
			
	if ( isset ( $_GET['year'] ) )
		if ( strlen($year) > 0 ) 
			$contTil .= ' Year: '.$year;
			
	if ( isset ( $_GET['genre'] ) )
		if ( count ( $genre ) > 0 ) {
			$contTil .= ' Genre: ';
			for ( $i = 0; $i < count ( $genre ); $i++ )
				$contTil .= ''.$genre[$i].', ';
			
			$contTil = substr( $contTil,0,strlen($contTil)-2);
		}

	if ( isset ( $_GET['actor'] ) )
		if ( strlen($actor) > 0 ) 
			$contTil .= ' Actor: '.$actor;
			
	if ( isset ( $_GET['director'] ) )
		if ( strlen($director) > 0 ) 
			$contTil .= ' Director: '.$director;
	$contTil .='"';
	echo "<script type=\"text/javascript\">document.getElementById(\"contentTitle\").innerHTML=".$contTil."</script>";
?>

<script type="text/javascript">

var ad = 'hide';

function showAdvancedSearch() {
	if ( ad == 'hide' ) { $( '#advanced' ).show ( 'slow' ); ad = 'show'; }
	else { $( '#advanced' ).hide ( 'slow' ); ad = 'hide'; }
	
}

</script>